package com.iona.servlets;

import static java.lang.Integer.valueOf;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.iona.dao.PartDisPurviewDAO;
import com.iona.domain.BaseEntity;
import com.iona.domain.PartDisPurview;
import com.iona.json.JSONException;
import com.iona.utils.CreatorUtil;
import com.iona.utils.Storage;

/**
 * Servlet implementation class AddPartDisPurviewServlet
 */
@WebServlet("/AddPartDisPurviewServlet")
public class AddPartDisPurviewServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	@Override
	public void init() throws ServletException {
		super.init();
		setTransactional(true);
	}

	@Override
	public void doExecute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			JSONException {
		String pdisid = request.getParameter("pdisid");
		String purid = request.getParameter("purid");
		if (pdisid == null || purid == null) {
			goToList(request, response);
			return;
		}
		Integer partDId = valueOf(pdisid);
		Integer purId = valueOf(purid);
		PartDisPurview pdp = new PartDisPurviewDAO()
				.findByPartDisAndPurview(partDId, purId);

		if (pdp == null) {
			PartDisPurview createPDP = CreatorUtil.createPDP(partDId, purId);
			List<BaseEntity> toCreate = new ArrayList<BaseEntity>();
			toCreate.add(createPDP);
			new Storage().storeAll(toCreate, null, null);
		}
		goToList(request, response);
	}

	

}